package com.tensquare.article.service;


import com.tensquare.article.pojo.Comment;
import com.tensquare.article.repository.CommentRepository;
import com.tensquare.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MongoTemplate mongoTemplate;

    public List<Comment> findAll() {
        return this.commentRepository.findAll();
    }

    public Comment findById(String commentId) {
        Optional<Comment> comment = this.commentRepository.findById(commentId);
        if (comment.isPresent()){
            //Optional可以减少空指针
            //optional.isPresent() 判断对象是否存在
            return comment.get();
        }
        return null;
    }

    public void save(Comment comment) {
        //id
        comment.set_id(idWorker.nextId()+"");
        //初始化
        comment.setThumbup(0);
        comment.setPublishdate(new Date());
        this.commentRepository.save(comment);
    }

    /**
    * Created by : 大萝卜
    * Date: 2020-4-6        restful格式
    */
    public void updateById(Comment comment) {
        this.commentRepository.save(comment);
    }

    public void deleteById(String commentId) {
        this.commentRepository.deleteById(commentId);
    }

    public List<Comment> findByArticleId(String articleId) {
       return this.commentRepository.findByArticleid(articleId);
    }

    //点赞
    public void thumbup(String commentId) {
        //性能低，操作数据库两次
//        Optional<Comment> optional = this.commentRepository.findById(commentId);
//        if (optional.isPresent()){
//            Comment comment = optional.get();
//            comment.setThumbup(comment.getThumbup()+1);
//            this.commentRepository.save(comment);
//        }
        //点赞功能优化（操作数据库1次）
        Query query = new Query();
        //条件
        query.addCriteria(Criteria.where("_id").is(commentId));
        //修改+1
        Update update = new Update();
        update.inc("thumbup",1);//+1
        this.mongoTemplate.updateFirst(query,update,"comment");
    }

    //取消点赞
    public void thumbups(String commentId) {
        Optional<Comment> optional = this.commentRepository.findById(commentId);
        if (optional.isPresent()){
            Comment comment = optional.get();
            comment.setThumbup(comment.getThumbup()-1);
            this.commentRepository.save(comment);
        }
    }
}
